#pragma once

class Pool;
class Manager;
struct Msg;

#include <vector>
#include <list>
#include <stdio.h>
#include "GameObject.h"
#include "RenderSystem.h"
#include "GameStats.h"

using namespace std;

struct Msg
{
	unsigned int uiIdObject;
	unsigned int uiIdPool;
	unsigned int uiEvent;

	union
	{
		struct //MSG_MOVE
		{
			float dx, dy;
		};

		struct //MSG_CREATE
		{
			float x, y, direct;
			unsigned int uiObjectType;
		};

		struct //MSG_DEATH
		{
			bool killed;
			bool respawn;
		};

		struct //MSG_UPDATE
		{
			unsigned int uiTime;
		};

		struct //MSG_KB
		{
			bool fKeyPressed;
			unsigned char Code;
		};

		struct //MSG_CHNGLVL
		{
			bool fWin;
		};

		struct //MSG_COLLISION
		{
			bool Reason;
			float dx, dy;
			bool Water;
			bool Border;
		};
	};
};

//--------------------------------------------------
class Pool
{
public:
	friend class Application;
	friend class Manager;
	Pool();
	void Init(Manager *, unsigned int pool_id);
	Pool(Manager*, unsigned int pool_id); //done
	~Pool();
	void Message(Msg Message);
	GameObject* GetObjectByID(unsigned int ID);//done
	unsigned int GetID(); //done
	void SetFilter(unsigned int Mask); //done
	Manager* mgr;

private:
	unsigned int ID;
	unsigned int Mask;
	unsigned int uiCurrentObject;
	unsigned int uiObjectCount;
	unsigned int GetNextObject();
	void Render();
	void Clear();
	list<GameObject*> ObjectList;

};
//-----------------------------------------------------------
class Manager
{
public:
	friend class Pool;
	friend class Application;
	Manager();										//done
	void Init(Application* ap);						//INitialization
	~Manager();
	unsigned int GenID();							//done
	void Message (Msg *);							//done
	GameObject* GetObjectByID(unsigned int ID);		//done
	Application * App;
	Terrain terrain;
	void AddExplosion(float x, float y);
	void RenderExplosions();

private:
	void ChangeLevel();                             //
	void Update(unsigned int t);					//done
	void LoadMap();									//done
	void LoadIndexFile(char * filename);			//done
	void ProcessMessages();							//done	
	void RenderAll();                               //done
	vector<Pool> Pools;
	list<Msg>Queue;
	list<Explosion>ExpList;
	char MapIndexFile[50][256];
	unsigned int uiMapCount;
	unsigned int uiCurrrentMap;
	unsigned int uiAICount, uiAIMax;
	unsigned int maxID;
	GameStats Statistic;	
};